{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 粗心导致的错误"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入你的年龄：99\n",
      "99 <class 'str'>\n",
      "成年人\n",
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'\\n粗心导致的错误：\\n1.漏了末尾的冒号，如if语句，循环语句，else子句等\\n2.缩进错误，该缩进的没缩进，不该缩进的瞎缩进\\n3.把英文符号写成中文符号，比如说：引号，冒号，括号\\n4.字符串拼接的时候，把字符串和数字拼在一起\\n5.没有定义变量，比如说while的循环条件的变量\\n6.==比较运算符和=赋值运算符混用了\\n'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age = input('请输入你的年龄：')\n",
    "print(age, type(age))\n",
    "if int(age) >= 18:\n",
    "    print('成年人')\n",
    "\n",
    "i = 0\n",
    "while i < 10:\n",
    "    print(i)\n",
    "    i += 1\n",
    "\n",
    "\"\"\"\n",
    "粗心导致的错误：\n",
    "1.漏了末尾的冒号，如if语句，循环语句，else子句等\n",
    "2.缩进错误，该缩进的没缩进，不该缩进的瞎缩进\n",
    "3.把英文符号写成中文符号，比如说：引号，冒号，括号\n",
    "4.字符串拼接的时候，把字符串和数字拼在一起\n",
    "5.没有定义变量，比如说while的循环条件的变量\n",
    "6.==比较运算符和=赋值运算符混用了\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. 知识点不熟导致的错误"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "44\n",
      "['A', 'B', 'C']\n"
     ]
    }
   ],
   "source": [
    "lst = [11, 22, 33, 44]\n",
    "print(lst[3])  # 4越界，从0开始\n",
    "\n",
    "lst1 = []\n",
    "# lst1.append('A', 'B', 'c')\n",
    "lst1.append('A')\n",
    "lst1.append('B')\n",
    "lst1.append('C')  # 一次一个\n",
    "print(lst1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3. 思路不清导致的错误"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入你要查询的演员：张国荣\n",
      "张国荣 出演了： 霸王别姬\n"
     ]
    }
   ],
   "source": [
    "lst = [{'rating':[9.7,2062397],'id':'1292052','type':['犯罪','剧情'],'title':'肖申克的救赎','actors':['蒂姆·罗宾斯','摩根·弗里曼']},\n",
    "      {'rating':[9.6,1528760],'id':'1291546','type':['剧情','爱情','同性'],'title':'霸王别姬','actors':['张国荣','张丰毅','巩俐','葛优']},  \n",
    "      {'rating':[9.5,1559181],'id':'1292720','type':['剧情','剧情'],'title':'阿甘正传','actors':['汤姆·汉克斯','罗宾·怀特']}]                   \n",
    "\n",
    "name = input('请输入你要查询的演员：')\n",
    "for item in lst: # 遍历列表 -> {}  item是一个又一个的字典\n",
    "    act_lst = item['actors']\n",
    "#     print(act_lst)\n",
    "    for actor in act_lst:\n",
    "        if name in actor:\n",
    "            print(name, '出演了：', item['title'])\n",
    "    \n",
    "#     for movie in item: # 遍历字典，得到movie是一个字典中的key\n",
    "#         print(movie)\n",
    "#         actors = movies['actors']\n",
    "        \n",
    "#         if name in actors:\n",
    "#             print(name+'出演了：', movie)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4. 异常处理机制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入第一个整数：32\n",
      "请输入第二个整数：27\n",
      "结果为： 1.1851851851851851\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    a = int(input('请输入第一个整数：'))\n",
    "    b = int(input('请输入第二个整数：'))\n",
    "    res = a / b\n",
    "    print('结果为：', res)\n",
    "except ZeroDivisionError:\n",
    "    print('除数不可以为0')\n",
    "except ValueError:\n",
    "    print('不能将字符串转换为数字')\n",
    "except BaseException as e:\n",
    "    print(e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入第一个整数：15\n",
      "请输入第二个整数：31\n",
      "计算结果为： 0.4838709677419355\n",
      "谢谢您的使用\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    a = int(input('请输入第一个整数：'))\n",
    "    b = int(input('请输入第二个整数：'))\n",
    "    result = a / b\n",
    "except BaseException as e:\n",
    "    print('出错了：', e)\n",
    "else:\n",
    "    print('计算结果为：', result)\n",
    "finally:\n",
    "    print('谢谢您的使用')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 5. 常见的异常类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.除（或取模）零 所有数据类型\n",
    "# print(10/0)  # ZeroDivisionError\n",
    "\n",
    "# 2.序列中没有此索引\n",
    "lst = [11, 22, 33, 44]\n",
    "# print(lst[4])  # IndexError\n",
    "\n",
    "# 3.映射中没有这个键\n",
    "dic = {'name': '张三', 'age': 20}\n",
    "# print(dic['gender'])  # KeyError\n",
    "\n",
    "# 4.未声明/初始化对象（没有属性）\n",
    "# print(num)  # NameError\n",
    "\n",
    "# 5.语法错误\n",
    "# int a=20  # SyntaxError\n",
    "\n",
    "# 6.传入无效的参数\n",
    "# a = int('hello')  # ValueError"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 6. traceback模块的使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-------------\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Traceback (most recent call last):\n",
      "  File \"<ipython-input-7-3a16cad73127>\", line 6, in <module>\n",
      "    print(1 / 0)\n",
      "ZeroDivisionError: division by zero\n"
     ]
    }
   ],
   "source": [
    "# print(10/0)\n",
    "import traceback\n",
    "\n",
    "try:\n",
    "    print('-------------')\n",
    "    print(1 / 0)\n",
    "except:\n",
    "    traceback.print_exc()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 7. 程序调试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "i = 1\n",
    "while i <= 10:\n",
    "    print(i)\n",
    "    i += 1"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.6.3",
   "language": "python",
   "name": "python3.6.3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "480px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
